﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using MINERP.DAL.PrintManage;
using MINERP.DAL;
using MINERP.COM;


namespace MINERP.BLL.PrintManage
{
    public class PaperWasteRecordBLL
    {
        public DataSet GetPrintPaperUsage(string pName, DateTime startTime, DateTime endTime)
        {
            DataSet ds = new DataSet();

            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                DataTable dt = GreatePrintPaperUsageTable();

                List<PaperInfo> paperInfoLis = entities.PaperInfo.Where(p => p.PaperYieldArea!=0&& p.printer.PName == pName && p.LastPrintTime >= startTime && p.LastPrintTime <= endTime).ToList();
                foreach (var item in paperInfoLis)
                {
                    double totalArea = (double)item.PaperConsumptionLength * (double)item.Width / 10000 + (double)item.PaperEndArea;
                    if (totalArea<=0)
                    {
                        continue;
                    }
                    double wasteArea = (double)item.PaperBreakArea + (double)item.PaperEndArea + (double)item.PaperTitleArea;
                    double useArea = Math.Round((double)item.PaperYieldArea, 4);
                    if (!item.paperFaceReference.IsLoaded)
                    {
                        item.paperFaceReference.Load();
                    }
                    if (!item.paperTypeReference.IsLoaded)
                    {
                        item.paperTypeReference.Load();
                    }
                    DataRow dr = dt.NewRow();
                    dr["paperId"] = item.PrintPaperId;
                    dr["paperFaceName"] = item.paperFace.Name;
                    dr["paperTypeName"] = item.paperType.Name;
                    dr["useArea"] = useArea;

                    dr["totalArea"] = Math.Round(totalArea,4);
                    dr["wasteArea"] = Math.Round(wasteArea,4);
                    dr["usagePer"] = Math.Round(useArea / totalArea, 2);//利用率
                    dr["papertitlePer"] = Math.Round(((double)item.PaperTitleArea)  / totalArea, 2);//纸头百分比
                    dr["paperEndPer"] = Math.Round(((double)item.PaperEndArea)  / totalArea, 2);//纸尾百分比
                    dr["paperBreakPer"] = Math.Round(((double)item.PaperBreakArea)  / totalArea, 2);//卡纸百分比
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt);
            }
            ds.WriteXml(@"C:\windows\xmlData\PrintPaperUsage.xml", XmlWriteMode.WriteSchema);
            return ds;
        }


        public List<printer> GetPrinterList()
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.printer.ToList();
            }
        }
        private DataTable GreatePrintPaperUsageTable()
        {
            DataTable dt = new DataTable();
            dt.TableName = "paperUse";

            dt.Columns.Add("paperId", typeof(int));
            dt.Columns.Add("paperFaceName",typeof(string));
            dt.Columns.Add("paperTypeName",typeof(string));
            dt.Columns.Add("useArea",typeof(float));
            dt.Columns.Add("totalArea",typeof(float));
            dt.Columns.Add("wasteArea",typeof(float));
            dt.Columns.Add("usagePer",typeof(float));
            dt.Columns.Add("papertitlePer", typeof(float));
            dt.Columns.Add("paperEndPer", typeof(float));
            dt.Columns.Add("paperBreakPer", typeof(float));
            return dt;
        }
    }
}
